package com.cjm.algorithm.leetcode;

public class RemoveDuplicatedElement2 {
    public static int removeDuplicates(int[] nums) {
        int count = 1;
        int cur = nums[0];
        int minitor = 0;
        for (int i = 1; i < nums.length; i++) {
            if (count == 0) {
                if (nums[i] == cur) {
                    minitor++;
                    nums[i] = Integer.MAX_VALUE;
                    continue;
                }
                count = 1;
                cur = nums[i];
                continue;
            }
            if (cur == nums[i]) {
                count++;
            } else {
                count = 1;
                cur = nums[i];
            }
            if (count == 2) {
                count = 0;
                cur = nums[i];
            }
        }

        for (int i = nums.length - 1; i >= 0; i--) {
            if (nums[i] == Integer.MAX_VALUE) {
                putToEnd(nums, i);
            }
        }
        return nums.length - minitor;
    }

    private static void putToEnd(int[] nums, int i) {
        for (int k = i; k + 1 < nums.length; k++) {
            int temp = nums[k];
            nums[k] = nums[k + 1];
            nums[k + 1] = temp;
        }
    }

    public static void main(String[] args) {
        int[] temp = new int[]{0,0,1,1,1,1,2,3,3};
        int i = removeDuplicates(temp);
        System.err.println("OK");
    }
}
